<template>
  <div id="app">
    <div id="root">
      <div class="todo-container">
        <div class="todo-wrap">
          <!-- 绑定自定义事件，用于数据传输 -->
          <NoteHeader @addtodo="addtodo" />
          <!-- 将数据通过绑定属性的方式传给子组件 -->
          <NoteList :todos="todos" />
          <!-- 绑定全选和清除自定义事件，并传输todos数据给子组件 -->
          <NoteFooter
            :todos="todos"
            @checkAllTodo="checkAllTodo"
            @clearAllTodo="clearAllTodo"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//引入发布订阅插件
import pubsub from "pubsub-js";
import NoteHeader from "./components/NoteHeader";
import NoteFooter from "./components/NoteFooter";
import NoteList from "./components/NoteList";

export default {
  name: "App",
  components: { NoteFooter, NoteHeader, NoteList },
  data() {
    return {
      //数据从本地获取，通过JSON.parse转换成数组
      todos: JSON.parse(localStorage.getItem("todos")) || [],
    };
  },
  methods: {
    //添加todoObj，
    addtodo(todoObj) {
      this.todos.unshift(todoObj);
    },
    //判断todo的状态,勾选todo时触发，遍历todos，匹配勾选id，取反todo的done值
    checkTodo(id) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    //删除todo，点击删除触发，发布订阅者模式，第一个参数为消息名，此处不需要使用_,
    delTodo(_, id) {
      this.todos = this.todos.filter((todo) => todo.id !== id);
    },
    //将所有todo.done设置为true，
    checkAllTodo(done) {
      this.todos.forEach((todo) => {
        todo.done = done;
      });
    },
    clearAllTodo() {
      this.todos = this.todos.filter((todo) => {
        return !todo.done;
      });
    },
    //更新
    updateTodo(id, title) {
      this.todos.forEach((todo) => {
        if (todo.id === id) todo.title = title;
      });
    },
  },
  watch: {
    //深度监视
    todos: {
      deep: true,
      handler(value) {
        localStorage.setItem("todos", JSON.stringify(value));
      },
    },
  },
  mounted() {
    this.$bus.$on("checkTodo", this.checkTodo);
    this.$bus.$on("updateTodo", this.updateTodo);
    // this.$bus.$on("delTodo", this.delTodo);
    this.pubId = pubsub.subscribe("delTodo", this.delTodo);
  },
  beforeDestroy() {
    this.$bus.$off("checkTodo");
    this.$bus.$off("updateTodo");
    // this.$bus.off("delTodo");
    pubsub.unsubscribe(this.pubId);
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-edit {
  color: #fff;
  background-color: skyblue;
  border: 1px solid rgb(58, 153, 190);
  margin-right: 5px;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn-edit:hover {
  color: #fff;
  background-color: rgb(84, 188, 230);
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
